<template>
    <div class="home-wrapper">
        <project-menu :detail="projectDetail" :pid="projectId"></project-menu>
        <div class="filters-wrapper">
            <div class="title total_title">{{title}}</div>
            <el-row type="flex" justify="start">
                <div class="newtask_warp">
                    <el-form :model="form" label-width="120px">
                        <el-form-item label="任务描述：">
                            <el-input type="textarea" v-model="form.content" :autosize="{ minRows: 4, maxRows: 6}">
                            </el-input>
                        </el-form-item>
                        <el-form-item label="执行人：">
                            <el-select v-model="form.id_user" placeholder="请选择" size="small">
                                <el-option v-for="item in projectDetail['users']" :key="item.id" :label="item.name" :value="item.id"></el-option>
                            </el-select>
                            <span style="color: #999;padding-left: 10px;">(本项目成员)</span>
                        </el-form-item>
                        <el-form-item label="完成时间：">
                            <el-date-picker size="small" v-model="form.finishtime" type="datetime" placeholder="选择日期时间" value-format="yyyy-MM-dd HH:mm:ss"> 
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item>
                            <el-button @click="$router.go(-1)">取消</el-button>
                            <el-button @click="saveTaskClick" type="primary">{{buttonTitle}}</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </el-row>
        </div>
    </div>
</template>
<style>

</style>
<script>
    export default {
        data() {
            return {
                projectId: 0,
                taskId: 0,
                isCopyTask: false,
                title: '新建任务',
                buttonTitle: '创建',
                projectDetail: {},
                form: {
                    id: 0,
                    projectid: 0,
                    content: '',
                    id_user: '',
                    finishtime: ''
                }
            }
        },
        created() {
            this.projectId = this.$router.currentRoute.params.pid
            this.taskId = this.$router.currentRoute.params.id
            let copyId = this.$route.query.copy
            if (this.taskId > 0) {
              this.title = '修改任务'
              this.form.id = this.taskId
              this.buttonTitle = '修改'
            } else if(copyId > 0) {
                this.title = '复制任务'
                this.buttonTitle = '复制'
                this.isCopyTask = true
                this.taskId = copyId
            }
            this.form.projectid = this.projectId
        },
        mounted() {
            this.loadData()
        },
        methods: {
            loadData() {
                this.$axios.get('/project/detail', {projectid: this.projectId})
                .then(e => {
                    // let data = e.data
                    console.log('项目详情', e.data)
                    this.projectDetail = e.data
                })
                if (this.taskId > 0) {
                  this.$axios.get('/task/detail', {projectid: this.projectId, taskid: this.taskId})
                  .then(e => {
                      this.form = e.data
                      if (this.isCopyTask) {
                          this.form.id = 0
                          this.taskId = 0
                      }
                  })
                }
            },
            saveTaskClick() {
                this.$axios.post('/task/save', this.form)
                .then((e) => {
                    console.log('save', this.form)
                    this.form['content'] = ''
                    this.$message({
                    message: e.msg ? e.msg : '保存成功',
                    type: 'success'
                    })
                    this.$router.replace({
                        path: '/tasks/' + this.projectId
                    })   
                })
            }
        }
    }
</script>
